//
//  CZQHomeCourseTCell.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/21.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQHomeCourseTCell.h"
#import "CourseClassInfoViewController.h"
@interface CZQHomeCourseTCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UICollectionViewFlowLayout *flowLayout;
}
@end
@implementation CZQHomeCourseTCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
	[self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self);
        
        
//            make.height.mas_equalTo(2 * _height + 20);
        
    }];
        
        
    }
    return self;
}

#pragma  mark -懒加载
//-(NSMutableArray *)dataArrM
//{
//    if (!_dataArrM) {
//        _dataArrM = [NSMutableArray array];
//    }
//
//    return _dataArrM;
//
//}




-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        flowLayout= [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向
        flowLayout.minimumLineSpacing = 20;//行间距(最小值)
        flowLayout.minimumInteritemSpacing = 10;//item间距(最小值)
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);//设置section的边距
        
        
        CGFloat width = kScreenWidth - flowLayout.sectionInset.left - flowLayout.sectionInset.right;
        CGFloat w= floorf((width - (2 - 1) * flowLayout.minimumInteritemSpacing) / 2);
        
        
        _height = w/4*3 + 90;
        
        flowLayout.itemSize = CGSizeMake(w, _height);
        
        //item的大小
//        flowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 20);
//        flowLayout.footerReferenceSize = CGSizeMake(kScreenWidth, 20);
//
        
        UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout ];
        //注册cell
        
        [collection registerNib:[UINib nibWithNibName:@"CZQHomeCourseCCell" bundle:nil] forCellWithReuseIdentifier:@"CZQHomeCourseCCell"];
        
        collection.scrollEnabled = NO;
        
        
        collection.backgroundColor = [UIColor whiteColor];
        //设置代理
        collection.delegate = self;
        collection.dataSource = self;
        [self addSubview:collection];
        
        
        
//        flowLayout.estimatedItemSize = CGSizeMake(w, w);
        
        _collectionView = collection;
        
    }
    return _collectionView;
}

#pragma mark ---- UICollectionViewDelegate && UICollectionViewDatasource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    
    return self.dataArrM.count ;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CZQHomeCourseCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CZQHomeCourseCCell" forIndexPath:indexPath];
    
    
    
    return cell;
}



//设置cell的大小


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    __block NSInteger countItem;
//    __block CGFloat w;
//    dispatch_block_t block = ^{
//        NSLog(@"%f",flowLayout.sectionInset.left);
//        CGFloat width = self.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right;
//        w= floorf((width - (countItem - 1) * flowLayout.minimumInteritemSpacing) / countItem);
//    };
//
//
//
//    countItem = 2;
//    block();
//
//
//    _height = w/4*3 + 90;
//
//
//    return CGSizeMake(w, w/4*3 + 90);
//
//}



//点击cell触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CourseClassInfoViewController *course = [CourseClassInfoViewController new];
    
    [self.viewController.navigationController pushViewController:course animated:YES];
}

//是否允许点击cell
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

#pragma mark - 数据处理
-(void)setDataArrM:(NSMutableArray *)dataArrM
{
    _dataArrM = dataArrM;
    
    NSInteger row = 0;

    if (dataArrM.count%2 == 0) {
        row = dataArrM.count/2 ;
    }else
    {
        row = dataArrM.count/2 + 1;

    }


    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.edges.mas_equalTo(self);
        make.height.mas_equalTo((_height + 20) *row);

    }];

//    [self layoutIfNeeded];
//    [self setNeedsUpdateConstraints];
//    [self updateConstraintsIfNeeded];
    
    [self.collectionView reloadData];
    
    
    
//    [self updateTheCollectionview:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
